//
//  CellForExploreSectionOne.m
//  echo
//
//  Created by dllo on 16/1/27.
//  Copyright © 2016年 Danny. All rights reserved.
//

#define WIDTH [UIScreen mainScreen].bounds.size.width / 414
#define HEIGHT [UIScreen mainScreen].bounds.size.height / 736

#import "CellForExploreSectionOne.h"
#import "ModelForExploreTableViewCell.h"
#import <SDWebImage/UIButton+WebCache.h>
#import <DKNightVersion.h>

@interface CellForExploreSectionOne ()
@property (nonatomic, retain)UIView *viewForBack;
@property (nonatomic, retain)UIButton *buttonForLeft;
@property (nonatomic, retain)UIButton *buttonForRight;
@property (nonatomic, retain)UILabel *labelForLeft;
@property (nonatomic, retain)UILabel *labelForRight;
@property (nonatomic, retain)NSArray *arrForData;
@property (nonatomic, retain)ModelForExploreTableViewCell *model1;
@property (nonatomic, retain)ModelForExploreTableViewCell *model2;
@end

@implementation CellForExploreSectionOne

- (void)dealloc{
    [_viewForBack release];
    [_buttonForLeft release];
    [_buttonForRight release];
    [_labelForLeft release];
    [_labelForRight release];
    [_arrForData release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.viewForBack = [[UIView alloc]init];
        [self.contentView addSubview:self.viewForBack];
        [self.viewForBack release];
        
        self.buttonForLeft = [UIButton buttonWithType:UIButtonTypeSystem];
        [self.viewForBack addSubview:self.buttonForLeft];

        self.buttonForRight = [UIButton buttonWithType:UIButtonTypeSystem];
        [self.viewForBack addSubview:self.buttonForRight];

        self.labelForLeft = [[UILabel alloc]init];
        [self.buttonForLeft addSubview:self.labelForLeft];
        [self.labelForLeft release];
        
        self.labelForRight = [[UILabel alloc]init];
        [self.buttonForRight addSubview:self.labelForRight];
        [self.labelForRight release];
        [self handleNightModel];
    }
    return self;
}

- (void)handleButtonForLeftOfExplore:(UIButton *)button{
    
    ModelForExploreTableViewCell *model = [self.arrForData objectAtIndex:0];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"postModelForLeftOfExploreVC" object:nil userInfo:@{@"model":model}];
}

- (void)handleButtonForRightOfExplore:(UIButton *)button{
    ModelForExploreTableViewCell *model = [self.arrForData objectAtIndex:1];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"postModelForRightOfExploreVC" object:nil userInfo:@{@"model":model}];
}

- (void)handleNightModel{
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"夜间模式"]) {
        
        [DKNightVersionManager nightFalling];
        
    } else {
        
        [DKNightVersionManager dawnComing];
    }
    self.viewForBack.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:17 / 256.f green:27 / 256.f blue:47 / 256.f alpha:1]);
    self.labelForLeft.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor whiteColor]);
    self.labelForRight.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor whiteColor]);
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    self.viewForBack.frame = self.contentView.frame;
    
    self.buttonForLeft.frame = CGRectMake(12 * WIDTH, 10 * HEIGHT, (self.contentView.frame.size.width - 36 * WIDTH) / 2, self.contentView.frame.size.height - 20 * HEIGHT);
    self.buttonForLeft.layer.cornerRadius = 8 * WIDTH;
    self.buttonForLeft.layer.masksToBounds = YES;
    
    self.buttonForRight.frame = CGRectMake((self.contentView.frame.size.width - 36 * WIDTH) / 2 + 24 * WIDTH, 10 * HEIGHT, (self.contentView.frame.size.width - 36 * WIDTH) / 2, self.contentView.frame.size.height - 20 * HEIGHT);
    self.buttonForRight.layer.cornerRadius = 8 * WIDTH;
    self.buttonForRight.layer.masksToBounds = YES;
    
    self.labelForLeft.frame = CGRectMake(0, self.contentView.frame.size.height - 20 * HEIGHT - 30 * HEIGHT, (self.contentView.frame.size.width - 36 * WIDTH) / 2, 30 * HEIGHT);
    self.labelForLeft.textAlignment = NSTextAlignmentCenter;
//    self.labelForLeft.textColor = [UIColor whiteColor];
    self.labelForLeft.font = [UIFont fontWithName:@"Arial" size:17.0];
    
    self.labelForRight.frame = CGRectMake(0, self.contentView.frame.size.height - 20 * HEIGHT - 30 * HEIGHT, (self.contentView.frame.size.width - 36 * WIDTH) / 2, 30 * HEIGHT);
    self.labelForRight.textAlignment = NSTextAlignmentCenter;
//    self.labelForRight.textColor = [UIColor whiteColor];
    self.labelForRight.font = [UIFont fontWithName:@"Arial" size:17.0];
}

- (void)passModelForTableViewCell:(NSArray *)arr{
    self.arrForData = [NSArray arrayWithArray:arr];
    self.model1 = [[ModelForExploreTableViewCell alloc]init];
    self.model1 = [arr objectAtIndex:0];
    
    self.model2 = [[ModelForExploreTableViewCell alloc]init];
    self.model2 = [arr objectAtIndex:1];
    
    [self.buttonForLeft sd_setBackgroundImageWithURL:[NSURL URLWithString:self.model1.pic] forState:UIControlStateNormal];
    [self.buttonForRight sd_setBackgroundImageWithURL:[NSURL URLWithString:self.model2.pic] forState:UIControlStateNormal];
    [self.buttonForLeft addTarget:self action:@selector(passModelForLeftButton) forControlEvents:UIControlEventTouchUpInside];
    [self.buttonForRight addTarget:self action:@selector(passModelForRightButton) forControlEvents:UIControlEventTouchUpInside];
    
    self.labelForLeft.text = self.model1.name;
    self.labelForRight.text = self.model2.name;
    
//    [model1 release];
//    [model2 release];
}

- (void)passModelForLeftButton {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"passModelForLeftButton" object:nil userInfo:@{@"model":self.model1}];
}

- (void)passModelForRightButton {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"passModelForRightButton" object:nil userInfo:@{@"model":self.model2}];
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
